﻿Imports System.Data.SqlClient

Public Class frmEmpresasUsuarios
    Dim Empresa As New clsEmpresasUsuarios
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub frmEmpresasUsuarios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaUsuarios()

        dgAutorizadas.Rows.Clear()
        dgDisponibles.Rows.Clear()

    End Sub
    Sub CargaUsuarios()
        lstUsuarios.Items.Clear()
        Dim cnConn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim strSql As String


        Try
            cnConn.ConnectionString = InitialConnection
            strSql = "SELECT NombreCompleto FROM Usuarios WHERE IdUsuario>1"
            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql

            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                lstUsuarios.Items.Add(rdBuscar("NombreCompleto"))
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Sub CargaEmpresasDisponibles()
        dgDisponibles.Rows.Clear()
        Dim cnConn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim strSql As String

        Dim IdUsuario As Integer = BuscarIntDatoInicial("SELECT IdUsuario FROM Usuarios WHERE NombreCompleto='" & lstUsuarios.Text & "'")

        Try
            cnConn.ConnectionString = InitialConnection
            strSql = "SELECT Nombre FROM Empresas WHERE IdEmpresa NOT IN (Select IdEmpresa FROM EmpresasUsuarios WHERE IdUsuario=" & IdUsuario & ") ORDER BY Nombre"
            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql

            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgDisponibles.Rows.Add(rdBuscar("Nombre"))
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Sub CargaEmpresasAutorizadas()
        dgAutorizadas.Rows.Clear()
        Dim cnConn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim strSql As String

        Dim IdUsuario As Integer = BuscarIntDatoInicial("SELECT IdUsuario FROM Usuarios WHERE NombreCompleto='" & lstUsuarios.Text & "'")

        Try
            cnConn.ConnectionString = InitialConnection
            strSql = "SELECT Nombre FROM Empresas WHERE IdEmpresa IN (Select IdEmpresa FROM EmpresasUsuarios WHERE IdUsuario=" & IdUsuario & ") ORDER BY Nombre"
            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql

            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgAutorizadas.Rows.Add(rdBuscar("Nombre"))
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub

    Private Sub lstUsuarios_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsuarios.SelectedIndexChanged
        CargaEmpresasDisponibles()
        CargaEmpresasAutorizadas()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim IdUsuario As Integer = BuscarIntDatoInicial("SELECT IdUsuario FROM Usuarios WHERE NombreCompleto='" & lstUsuarios.Text & "'")
        Dim IdEmpresa As Integer = BuscarIntDatoInicial("SELECT IdEmpresa FROM Empresas WHERE Nombre='" & dgDisponibles.CurrentRow.Cells(0).Value & "'")

        Empresa.IdUsuario = IdUsuario
        Empresa.IdEmpresa = IdEmpresa
        Empresa.Insertar()

        CargaEmpresasAutorizadas()
        CargaEmpresasDisponibles()


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim IdUsuario As Integer = BuscarIntDatoInicial("SELECT IdUsuario FROM Usuarios WHERE NombreCompleto='" & lstUsuarios.Text & "'")
        Dim IdEmpresa As Integer = BuscarIntDatoInicial("SELECT IdEmpresa FROM Empresas WHERE Nombre='" & dgAutorizadas.CurrentRow.Cells(0).Value & "'")

        Call SetSqlInicial("DELETE FROM EmpresasUsuarios WHERE IdUsuario=" & IdUsuario & " AND IdEmpresa=" & IdEmpresa)

        CargaEmpresasAutorizadas()
        CargaEmpresasDisponibles()
    End Sub
End Class